﻿@model int
@using Nop.Core;
@using Nop.Web.Framework.UI;
@{
    //register CSS and JS
    Html.AddCssFileParts(@Url.Content("~/Administration/Content/uploadify.css"));
    Html.AddScriptParts(@Url.Content("~/Administration/Scripts/swfobject.js"));
    Html.AddScriptParts(@Url.Content("~/Administration/Scripts/jquery.uploadify.v2.1.4.min.js"));
    
    //other variables
    var random = CommonHelper.GenerateRandomInteger();
    var clientId = "picture" + random;
    var pictureService = EngineContext.Current.Resolve<Nop.Services.Media.IPictureService>();
    var picture = pictureService.GetPictureById(Model);
}
<div id="@(clientId + "value")">
    @Html.HiddenFor(x => x)
</div>
<div id="@(clientId + "image")">
    <img src="@(pictureService.GetPictureUrl(Model, 100, true))" />
</div>
@if (picture != null)
{
    <span id="@(clientId + "remove")" class="t-button">@T("Admin.Picture.RemovePicture")</span>
}
else
{
    <span id="@(clientId + "remove")" class="t-button" style="display:none;">@T("Admin.Picture.RemovePicture")</span>
}
<br />
<input type="file" id="@clientId"/>
<script type="text/javascript">
    $(function () {
        $(function () {

            var authToken = '@(this.Context.Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)';

            $("#@clientId").uploadify({
                "uploader": "@(Url.Content("~/Content/Flash/uploadify.swf"))",
                "script": "@(Url.Content("~/Admin/Picture/AsyncUpload"))",
                "scriptData": { authToken: authToken },
                "multi": "false",
                "auto": true,
                "buttonText": "Upload",
                "cancelImg": "@Url.Content("~/Administration/Content/images/cancel-uploading.png")",
                "onComplete": function (event, ID, fileObj, response, data) {
                    var json = jQuery.parseJSON(response);
                    if (json.success) {
                    $("#@(clientId + "image")").html("<img src='" + json.imageUrl + "'/>");
                    $("#@(clientId + "value") input").val(json.pictureId);
                    $("#@(clientId + "remove")").show();
                    } else {
                        alert(json.error);
                    }
                }
            });

            $("#@(clientId + "remove")").click(function(e){
                $("#@(clientId + "image")").html("<img src='@pictureService.GetDefaultPictureUrl()'/>");
                $("#@(clientId + "value") input").val(0);
                $(this).hide();
            });

        });
    });
</script>
